<?php

namespace app\mini\controller\v1\client;

use app\common\library\MemberVo;
use app\common\logic\OrderLogic;
use app\mini\controller\Common;
use think\Request;
use think\response\Json;

class Order extends Common
{

    public $OrderLogic;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->OrderLogic = new OrderLogic();
    }

    /**
     * 订单确认
     * @return Json
     */
    public function order_confirm(): Json
    {
        $res = $this->exception(function () {
            $params = request()->param();
            $params['member_id'] = MemberVo::instance()->getMemberId();
            return $this->OrderLogic->order_confirm($params);
        });
        return $this->response($res);
    }



    /**
     * 订单详情
     * @return Json
     */
    public function detail(): Json
    {
        $res = $this->exception(function () {
            $params = request()->param();
            $params['member_id'] = MemberVo::instance()->getMemberId();
            return $this->OrderLogic->detail($params);
        });
        return $this->response($res);
    }

    /**
     * 提交订单
     * @return Json
     */
    public function submit(): Json
    {
        $res = $this->transException(function () {
            return $this->OrderLogic->submit(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 订单列表
     * @return Json
     */
    public function index(): Json
    {
        $res = $this->exception(function () {
            $params = request()->param();
            $params['member_id'] = MemberVo::instance()->getMemberId();
            return $this->OrderLogic->index($params);
        });
        return $this->response($res);
    }

    /**
     * 取消订单
     * @return Json
     */
    public function cancel(): Json
    {
        $res = $this->transException(function () {
            $order = $this->OrderLogic->first(['id'  => request()->param('order_id'), 'member_id' => $this->member_info_arr['member_id'],]);
            $this->OrderLogic->cancelOrder($order);
        });
        return $this->response($res);
    }

    /**
     * 订单支付
     * @return Json
     */
    public function pay(): Json
    {
        $res = $this->transException(function () {
            $params = request()->param();
            return $this->OrderLogic->payOrder((int)$params['order_id'], (int)$params['pay_type']);
        });
        return $this->response($res);
    }

    /**
     * 订单金额计算
     * @return Json
     */
    public function calculate(): Json
    {
        $res = $this->exception(function () {
            return $this->OrderLogic->calculate(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 订单支付方式信息
     * @return Json
     */
    public function orderPayMethodInfo()
    {
        $res = $this->exception(function () {
            return $this->OrderLogic->orderPayMethodInfo(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 通过支付方式计算金额
     * @return Json
     */
    public function calculateByPayType(): Json
    {
        $res = $this->exception(function () {
            $params = request()->param();
            $order = $this->OrderLogic->orderExist($params['order_id']);
            return $this->OrderLogic->calculateByPayType($order, (int)$params['pay_type']);
        });
        return $this->response($res);
    }

    /**
     * 申请退款
     * @return Json
     */
    public function applyRefund(): Json
    {
        $res = $this->exception(function () {
            return $this->OrderLogic->applyRefund(request()->param());
        });
        return $this->response($res);
    }

    /**
     * 退款详情
     * @return Json
     */
    public function refundDetail(): Json
    {
        $res = $this->exception(function () {
            return $this->OrderLogic->refundDetail(request()->param());
        });
        return $this->response($res);
    }

    public function waitOpenBillOrders()
    {
        $res = $this->exception(function () {
            $params['member_id'] = MemberVo::instance()->getMemberId();
            $params['status_in'] = [\app\common\model\Order::ORDER_STATUS_WAIT_COMMENT,\app\common\model\Order::ORDER_STATUS_OK];
            $params['bill_id'] = 0;
            $params['id'] = request()->param()['id'] ?? 0;
            $params['pay_amount'] = 1;//设置条件为 实际支付金额大于0
            return $this->OrderLogic->waitOpenBillOrders($params);
        });
        return $this->response($res);
    }


}
